package com.tanhua.server.service;

import com.tanhua.commons.constant.Constants;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CommentService {

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 评论列表查询
     * @param page
     * @param pageSize
     * @param movementId
     * @return
     */
    public PageResult<CommentVo> findByUserId(Long page, Long pageSize, String movementId) {

        // 通过动态id调用服务分页查询评论列表
        PageResult<Comment> commentPageResult = commentApi.findByUserId(page,pageSize,movementId);

        // 获取所有评论者ids 调用服务批量查询评论者信息
        List<CommentVo> commentVoList = new ArrayList<>();
        List<Long> commentIds = new ArrayList<>();
        List<Comment> commentList = commentPageResult.getItems();

//        for (Comment comment : commentList) {
//            // 取出所用评论者ids
//            commentIds.add(comment.getUserId());
//        }

        // 取出所用评论者ids
        commentIds = commentList.stream().map(Comment::getUserId).collect(Collectors.toList());

        // 批量查询评论者信息
        Map<Long, UserInfo> userInfoMap = userInfoApi.findByIds(commentIds, null);

        // 基于评论者信息与评论信息构造vo返回
//        commentVoList = commentList.stream().map(comment -> CommentVo.init(
//                userInfoMap.get(comment.getUserId()),comment)).collect(Collectors.toList());

        for (Comment comment : commentList) {
            CommentVo commentVo = CommentVo.init(userInfoMap.get(comment.getUserId()), comment);

            // 查询redis中是否有点赞记录，如果有，则将点赞设置为1
            String key = Constants.COMMENTS_INTERACT_KEY + comment.getId().toHexString();
            String hashKey = Constants.COMMENTS_LIKE_HASHKEY + UserHolder.getUserId();
            if(stringRedisTemplate.opsForHash().hasKey(key,hashKey)){
                commentVo.setHasLiked(1);
            }
            commentVoList.add(commentVo);
        }

        return new PageResult<>(page,pageSize,commentPageResult.getCounts(),commentVoList);
    }

    /**
     * 发布评论
     * @param movementId
     * @param comment
     */
    public void comment(String movementId, String comment) {
        // 构造评论对象 调用服务发表评论
        Comment com = new Comment();
        com.setContent(comment);    // 设置评论信息内容
        com.setCommentType(CommentType.COMMENT.getType()); // 设置状态为评论
        com.setUserId(UserHolder.getUserId());  // 设置评论人的id
        com.setPublishId(new ObjectId(movementId)); // 设置动态id

        commentApi.add(com);
    }


    /**
     * 评论点赞
     * @param commentId
     * @return
     */
    public Integer like(String commentId) {
        // 构建comment对象
        Comment com = new Comment();
        com.setId(new ObjectId(commentId));
        com.setUserId(UserHolder.getUserId());

        Integer count = commentApi.updateAdd(com);

        //2、拼接redis的key，将用户的点赞状态存入redis,代表用户已经点赞过了
        // 使用hash结构存储
        String key = Constants.COMMENTS_INTERACT_KEY + commentId;
        String hashKey = Constants.COMMENTS_LIKE_HASHKEY + UserHolder.getUserId();
        stringRedisTemplate.opsForHash().put(key,hashKey,"1");

        return count;
    }

    /**
     * 评论取消点赞
     * @param commentId
     * @return
     */
    public Integer dislike(String commentId) {
        // 构建comment对象
        Comment com = new Comment();
        com.setId(new ObjectId(commentId));
        com.setUserId(UserHolder.getUserId());

        Integer count = commentApi.updateDel(com);

        //2、拼接redis的key，将用户的点赞状态存入redis,代表用户已经点赞过了
        // 使用hash结构存储
        String key = Constants.COMMENTS_INTERACT_KEY + commentId;
        String hashKey = Constants.COMMENTS_LIKE_HASHKEY + UserHolder.getUserId();
        stringRedisTemplate.opsForHash().delete(key,hashKey);

        return count;
    }
}
